package com.kavenka.zhufengfm.app.http;

import android.os.Build;
import com.kavenka.zhufengfm.app.utils.StreamUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.zip.GZIPInputStream;

/**
 * Created by kavenka on 2015/9/25 0025.
 */
public class HttpUtil {

    public static final int HTTP_CONNECT_TIMEOUT = 5000;
    public static final int HTTP_READ_TIMEOUT = 60000;
    private static final String USER_AGENT = "ting_4.1.7(" + Build.MODEL + ",Android" + Build.VERSION.SDK_INT + ")";

    public HttpUtil() {
    }


    /**
     * 返回流的网络GET请求
     * @param url
     * @return
     */
    public static InputStream doGetInputStream(String url){
        InputStream ret=null;
        if (url!=null){
            HttpURLConnection conn=null;
            InputStream inputStream=null;
            try {
                URL u=new URL(url);
                conn= (HttpURLConnection) u.openConnection();
                conn.setRequestMethod("GET");
                //通用的Http请求信息
                setCommonHttpInfo(conn);
                conn.connect();
                int code = conn.getResponseCode();
                if (code==HttpURLConnection.HTTP_OK){
                    ////////////////////响应部分
                    inputStream=conn.getInputStream();
                    //1.检查内容是否经过压缩,一般Json数据都会压缩
                    String encoding = conn.getContentEncoding();
                    if (encoding.equals("gzip")){
                        inputStream=new GZIPInputStream(inputStream);
                    }
                    ret=inputStream;
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }//返回的是流，不能关闭流
        }
         return ret;
    }

    /**
     * 返回字节数组的GET网络请求
     * @param url
     * @return
     */
    public static byte[] doGet(String url){
        byte[] ret=null;
        if (url!=null){
            HttpURLConnection conn=null;
            InputStream inputStream=null;
            try {
                URL u=new URL(url);
                conn= (HttpURLConnection) u.openConnection();
                conn.setRequestMethod("GET");
                //通用的Http请求信息
                setCommonHttpInfo(conn);
                conn.connect();
                int code = conn.getResponseCode();
                if (code==HttpURLConnection.HTTP_OK){
                    ////////////////////响应部分
                    inputStream=conn.getInputStream();
                    //1.检查内容是否经过压缩,一般Json数据都会压缩
                    String encoding = conn.getContentEncoding();
                    if (encoding.equals("gzip")){
                        inputStream=new GZIPInputStream(inputStream);
                    }
                    ret= StreamUtil.readStream(inputStream);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                StreamUtil.close(conn);
                conn=null;
                StreamUtil.close(inputStream);
                inputStream=null;
            }
        }
        return ret;
    }

    /**
     * POST请求
     * @param url
     * @param params
     * @return
     */
    public static byte[] doPost(String url,HashMap<String,String> params){
        byte[] ret=null;
        if (url != null) {

        }
        return ret;
    }

    /**
     * DELETE请求
     * @param url
     * @param params
     * @return
     */
    public static byte[] doDelete(String url,HashMap<String,String> params){
        byte[] ret=null;
        if (url != null) {

        }
        return ret;
    }

    /**
     * PUT请求
     * @param url
     * @param params
     * @return
     */
    public static byte[] doPut(String url,HashMap<String,String> params){
        byte[] ret=null;
        if (url != null) {

        }
        return ret;
    }
    /////////////////////////////////////
    /**
     * 设置通用的Http请求信息
     * @param conn
     */
    private static void setCommonHttpInfo(HttpURLConnection conn) {
        //设置连接和读取的超时时间
        conn.setConnectTimeout(HTTP_CONNECT_TIMEOUT);
        conn.setReadTimeout(HTTP_READ_TIMEOUT);
        //设置User-Agent字段"ting_4.1.7(手机型号，AndroidAPILevel)"
        conn.setRequestProperty("User-Agent",USER_AGENT);
        //设置客户端能够接收服务器传回的什么格式类型的内容
        conn.setRequestProperty("Accept","application/json,application/*,image/*,text/*,*/*");
        //设置客户端支持接收的压缩格式
        conn.setRequestProperty("Accept-Encoding","gzip");
    }
}
